package com.yimi.yinhepda.db.dao;

import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.yimi.yinhepda.base.BaseApplication;
import com.yimi.yinhepda.db.helper.DbHelper;
import com.yimi.yinhepda.db.table.DispatchScanTable;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 000432
 * @date 2017/4/22 0022
 * <p>
 * 派送扫描表
 */
public class DispactchScanTableDao extends BaseDao<DispatchScanTable, Long> {

    public DispactchScanTableDao() {
        super(DbHelper.getInstance(BaseApplication.getContext()), DispatchScanTable.class);
    }

    /**
     * 根据子单号查询相应的数据
     */
    public List<DispatchScanTable> findDataByChildNo(Long wayBillNo, String childNo, String taskNo) {
        try {
            QueryBuilder<DispatchScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("waybillNo", wayBillNo).and().eq("childNo", childNo).and().eq("taskNo", taskNo);
            List<DispatchScanTable> mDataList = queryBuilder.query();
            return mDataList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 判断该任务中的子单号是否存在
     *
     * @param childNo 子单号
     * @param taskNo  任务号
     * @return 存在返回true
     */
    public boolean exitDataByChildWaybillNo(String childNo, String taskNo) {
        try {
            QueryBuilder<DispatchScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("childNo", childNo).and().eq("taskNo", taskNo);
            List<DispatchScanTable> mDataList = queryBuilder.query();
            if (mDataList != null && mDataList.size() > 0) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 根据主单号查询相应的数据
     */
    public List<DispatchScanTable> findDataByWayBillNo(Long wayBillNo, String taskno) {
        try {
            QueryBuilder<DispatchScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("waybillNo", wayBillNo).and().eq("taskNo", taskno);
            List<DispatchScanTable> mDataList = queryBuilder.query();
            return mDataList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询未上传的数量
     */
    public int queryNotUploadDataNum() {
        int totalNum = 0;
        List<DispatchScanTable> mTableList;
        try {
            mTableList = super.findAll();
            if (mTableList != null && mTableList.size() > 0) {
                for (DispatchScanTable itemTable : mTableList) {
                    int isUpload = itemTable.getIsUpload();
                    if (isUpload == 0) {
                        totalNum++;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return totalNum;
    }

    /**
     * 查询未上传的数据
     */
    public List<DispatchScanTable> getUnUploadDataList(String taskNo) {
        try {
            QueryBuilder<DispatchScanTable, Long> queryBuilder = dao.queryBuilder();
            /**
             * 查询字段“isUpload”等于0的条件数据，
             *
             * 相当于sql语句"select * from 'buildPckScanTable' where 'isUpload' == 0 "
             *
             */
            List<DispatchScanTable> dispatchScanTabelList = queryBuilder.where()
                    .eq("isUpload", 0)
                    .and()
                    .eq("taskNo", taskNo)
                    .query();

            return dispatchScanTabelList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 更新表数据对象
     */
    public int updataItem(DispatchScanTable itemTable) {
        int result = -1;
        try {
            result = this.update(itemTable);
        } catch (SQLException e) {
            e.printStackTrace();
            result = -1;
        }
        return result;
    }

    /**
     * 删除根据子单号
     */
    public int delelteByChildBill(String childBill) {
        int result = -1;
        try {
            DeleteBuilder<DispatchScanTable, Long> deleteBuilder = dao.deleteBuilder();
            deleteBuilder.where().eq("childNo", childBill);
            result = deleteBuilder.delete();
        } catch (Exception e) {
            e.printStackTrace();
            result = -1;
        }
        return result;
    }

    /**
     * 根据任务编号删除
     */
    public int deleteByTaskNo(String taskNo) {
        int result = -1;
        try {
            DeleteBuilder<DispatchScanTable, Long> deleteBuilder = dao.deleteBuilder();
            deleteBuilder.where().eq("taskNo", taskNo);
            result = deleteBuilder.delete();
        } catch (Exception e) {
            e.printStackTrace();
            result = -1;
        }
        return result;
    }

    /**
     * @return
     */
    public int getScannedDataList(String waybillNo) {
        try {
            QueryBuilder<DispatchScanTable, Long> queryBuilder = dao.queryBuilder();
            List<DispatchScanTable> dispatchScanListData = queryBuilder.where().eq("waybillNo", waybillNo).query();
            return dispatchScanListData.size();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 根据任务号查询数据
     */
    public boolean exitDataByTaskNo(String taskNo) {
        try {
            QueryBuilder<DispatchScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("taskNo", taskNo);
            DispatchScanTable mDataList = queryBuilder.queryForFirst();
            if (mDataList != null) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
